package com.uchengwang.ui.cm_ui.umsg.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.ClubConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.functions.http.manager.OSSManager;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.basefm.BaseFragment;
import com.uchengwang.ui.base_ui.basefm.FragmentUMe;
import com.uchengwang.ui.base_ui.listener.HasUnReadMsgListener;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.cm_ui.me.presenter.HttpMeManager;
import com.uchengwang.ui.cm_ui.me.ui.ClubManagerActivity;
import com.uchengwang.ui.cm_ui.me.ui.LinkCycleActivity;
import com.uchengwang.ui.cm_ui.me.ui.MyClubInfoActivity;
import com.uchengwang.ui.cm_ui.ucity.ui.QRCodeShowActivity;
import com.uchengwang.ui.cm_ui.ucity.ui.UsersActivity;
import com.uchengwang.ui.cm_ui.umsg.bean.SchoolClubBean;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mizlicai on 2017/9/3.
 * 简约版社团 我的界面
 */

public class ClubFragmentMe extends BaseFragment implements View.OnClickListener {
    private View view;
    private PtrClassicFrameLayout layout;
    private ImageView club_head, fmMe_left_image;
    private TextView club_name, club_desc;
    private TextView fmMe_fansNum, fmMe_attenNum, fmMe_friNum, fmMe_qunNum;
    private RelativeLayout fmMe_club_manager, fmMe_club_link, fmMe_club_speek, fmMe_club_collect, fmMe_club_release, fmMe_club_apply, fmMe_club_info, fmMe_club_member, fmMe_club_access;
    private LinearLayout fansNum_linear, attenNum_linear, friNum_linear, qunNum_linear;

    //社团显示未读消息
    private ImageView manager_image_noRead, link_image_noRead, club_info_image, club_access_noRead, club_share_noRead, club_activity_noRead, club_member_noRead;
    private RelativeLayout club_info_rl, fmMe_club_transfer;

    private long refreshTime;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setLayoutId(R.layout.fragment_club_me);
        view = super.onCreateView(inflater, container, savedInstanceState);

        initView(view);
        return view;
    }

    private void initView(View view) {
        layout = view.findViewById(R.id.fmMe_layout);
        layout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (UCommonUtil.getUtil().getId() >0){
                    getMyInfo();
                    getMyGroup();
                    initCount();
                }else {
                    layout.refreshComplete();
                }
            }
        });

        club_info_rl = view.findViewById(R.id.club_info_rl);

        club_head = view.findViewById(R.id.club_head);
        club_head.setOnClickListener(this);
        club_name = view.findViewById(R.id.club_name);

        fmMe_left_image = view.findViewById(R.id.fmMe_left_image);
        fmMe_left_image.setOnClickListener(this);
//        club_desc = (TextView) view.findViewById(R.id.club_desc);

        fansNum_linear = view.findViewById(R.id.fmMe_fansNum_linear);
        fansNum_linear.setOnClickListener(this);
        attenNum_linear = view.findViewById(R.id.fmMe_attenNum_linear);
        attenNum_linear.setOnClickListener(this);
//        friNum_linear = (LinearLayout) view.findViewById(R.id.fmMe_friNum_linear);
//        friNum_linear.setOnClickListener(this);
//        qunNum_linear = (LinearLayout) view.findViewById(R.id.fmMe_qunNum_linear);
//        qunNum_linear.setOnClickListener(this);
        fmMe_fansNum = view.findViewById(R.id.fmMe_fansNum);
        fmMe_attenNum = view.findViewById(R.id.fmMe_attenNum);
//        fmMe_friNum = (TextView) view.findViewById(R.id.fmMe_friNum);
//        fmMe_qunNum = (TextView) view.findViewById(R.id.fmMe_qunNum);

//        fmMe_club_access = (RelativeLayout) view.findViewById(R.id.fmMe_club_access);
//        fmMe_club_access.setOnClickListener(this);
//        fmMe_club_speek = (RelativeLayout) view.findViewById(R.id.fmMe_club_speek);
//        fmMe_club_speek.setOnClickListener(this);
//        fmMe_club_collect = (RelativeLayout) view.findViewById(R.id.fmMe_club_collect);
//        fmMe_club_collect.setOnClickListener(this);
        club_info_image = view.findViewById(R.id.club_info_image);
        club_info_image.setOnClickListener(this);

        fmMe_club_manager = view.findViewById(R.id.fmMe_club_manager);
        fmMe_club_manager.setOnClickListener(this);
        manager_image_noRead = view.findViewById(R.id.fmMe_club_manager_image_noRead);

        fmMe_club_link = view.findViewById(R.id.fmMe_club_link);
        fmMe_club_link.setOnClickListener(this);
        link_image_noRead = view.findViewById(R.id.fmMe_club_link_image_noRead);

        fmMe_club_release = view.findViewById(R.id.fmMe_club_release);
        fmMe_club_release.setOnClickListener(this);
//        fmMe_club_apply = (RelativeLayout) view.findViewById(R.id.fmMe_club_apply);
//        fmMe_club_apply.setOnClickListener(this);
//        fmMe_club_info = (RelativeLayout) view.findViewById(R.id.fmMe_club_info);
//        fmMe_club_info.setOnClickListener(this);
//        fmMe_club_member = (RelativeLayout) view.findViewById(R.id.fmMe_club_member);
//        fmMe_club_member.setOnClickListener(this);

//        fmMe_club_transfer = (RelativeLayout) view.findViewById(R.id.fmMe_club_transfer);//转让社团管理权
//        fmMe_club_transfer.setOnClickListener(this);

//        club_access_noRead = (ImageView) view.findViewById(R.id.fmMe_club_speek_access_noRead);//纳新
//        club_share_noRead = (ImageView) view.findViewById(R.id.fmMe_club_speek_image_noRead);//分享
        club_activity_noRead = view.findViewById(R.id.fmMe_club_release_image_noRead);//发布
//        club_member_noRead = (ImageView) view.findViewById(R.id.fmMe_club_member_image_noRead);//成员

        layout.postDelayed(new Runnable() {

            @Override
            public void run() {
                layout.autoRefresh(true);
            }
        }, 150);
    }

    @Override
    public void onResume() {
        super.onResume();

        if (((System.currentTimeMillis() -refreshTime)/1000/60) >1 || FragmentUMe.isRefresh){
            //1分钟刷新一次
            initCount();
        }

        //更新群组数量
        if (BaseConfig.changeGroup){
            getMyGroup();
        }

        if(!TextUtils.isEmpty(UChengApplication.userAvatar)){
            ImageManager.getInstance().loadImage(ClubFragmentMe.this.getContext(), club_head, UChengApplication.userAvatar, ImageManager.ImageType.CIRCLE);
            club_name.setText(UChengApplication.nickName);
        }else {
            ImageManager.getInstance().loadImage(ClubFragmentMe.this.getContext(), club_head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
        }
    }


    /**
     * 查看某个人的信息
     * */
    private void getMyInfo(){
        if (!UCommonUtil.getUtil().isLogin()){
            return;
        }
        MapRequest map = new MapRequest();
        map.put(BaseConfig.ID, UChengApplication.clubId);
        HttpManager.newInstance(this.getActivity()).query(ClubConfig.Club.method, ClubConfig.Club.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                layout.refreshComplete();
                UCommonUtil.getUtil().showToast(ClubFragmentMe.this.getContext(), msg);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                layout.refreshComplete();
                if (status == 0){
                    if (TextUtils.isEmpty(data)){
                        return;
                    }

                    SchoolClubBean bean = JsonUtils.getManager().stringToBean(data, SchoolClubBean.class);
                    if (bean == null){
                        return;
                    }

                    String headLogo = bean.getLogoImg();
                    club_name.setText(bean.getOrgName());
//                    club_desc.setText(bean.getOrgSign());

                    if (!TextUtils.isEmpty(headLogo)){
                        ImageManager.getInstance().loadImage(ClubFragmentMe.this.getContext(), club_head, headLogo, ImageManager.ImageType.CIRCLE);
                    }else {
                        ImageManager.getInstance().loadImage(ClubFragmentMe.this.getContext(), club_head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                    }
                }else {
                    ImageManager.getInstance().loadImage(ClubFragmentMe.this.getContext(), club_head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                }
            }
        });

    }

    private void initCount(){
        refreshTime = System.currentTimeMillis();
        if (!UCommonUtil.getUtil().isLogin()){
            return;
        }
        HttpMeManager.getPresenter().checkMyCount(this.getActivity(), new HttpMeManager.CheckMyCountListener() {
            @Override
            public void onResult(int fans, int follow, int friend) {
                fmMe_fansNum.setText(fans+"");
                fmMe_attenNum.setText(follow+"");
//                fmMe_friNum.setText(friend+"");
            }
        });

        getUnReadMsg(null);
    }


    /**
     * 获取未读消息
     * */
    public void getUnReadMsg(final HasUnReadMsgListener listener){
        if (!UCommonUtil.getUtil().isLogin()){
            return;//未登录
        }
//        HttpManager.newInstance(this.getActivity()).query(UCityConfig.MyUnReadMsg.method, UCityConfig.MyUnReadMsg.URL, null, new HttpCallBack() {
//            @Override
//            public void onError(int status, String msg) {
//                Log.d("msg", "stutas="+status+"=msg="+msg);
//            }
//
//            @Override
//            public void onResp(int status, String msg, String data) {
//                Log.d("msg", "stutas="+status+"=msg="+msg);
//                if (!TextUtils.isEmpty(data)){
//                    UnReadMsgBean bean = JsonUtils.getManager().stringToBean(data, UnReadMsgBean.class);
//                    if (bean != null){
//                        //在ClubMainActivity里使用
//                        if (listener != null){
//                            int count = bean.getPublishNotice() + bean.getUnreadRecruit() + bean.getUnreadMsg();
//                            listener.unReadMsg(count);
//                        }
//
//                        if (UChengApplication.userRole == BaseConfig.CLUB_ROLE){
//                            if (bean.getUnreadRecruit() >0 || bean.getUnreadMsg() >0){
//                                //发布信息未读消息大于0
//                                if (club_access_noRead != null){
//                                    club_access_noRead.setVisibility(View.VISIBLE);
//                                    ((ClubMainActivity)ClubFragmentMe.this.getActivity()).showDot(4);
//                                }
//                            }
//                        }
//                    }
//
//                }
//            }
//        });

        //获取发布未读信息
//        UUHttpManager.getManager().getCmtMsg(getActivity(), 1, 1, false, new UUHttpManager.CmtMsgListener() {
//            @Override
//            public void onResult(int stutas, int totalPages, List<CmtmsgBean> listBean) {
//                if ( totalPages >0 && club_activity_noRead != null){
//                    if (listBean != null && listBean.size() >0){
//                        CmtmsgBean b = listBean.get(0);
//                        if (!b.isRead()){
//                            club_activity_noRead.setVisibility(View.VISIBLE);
//                            ((ClubMainActivity)ClubFragmentMe.this.getActivity()).showDot(4);
//                        }
//
//                    }
//
//                }
//            }
//        });

    }

    /**
     * 获取我的群组信息
     * */
    private void getMyGroup(){
//        RyGroupManager.getManager().getMyGroups(this.getActivity(), 1,1, 0, new OnHttpResultListener<RyGroupBean>() {
//            @Override
//            public void onResult(int status, String msg, int total, List<RyGroupBean> list) {
//                if (!TextUtils.isEmpty(msg)){
//                    fmMe_qunNum.setText(msg);//为了方便展示群数量 暂时将群数量放在了msg里
//                }else {
//                    fmMe_qunNum.setText(0+"");
//                }
//
//            }
//        });
    }


    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.fmMe_left_image:
                //查看社团信息
                intent = new Intent(this.getContext(), MyClubInfoActivity.class);
                break;

            case R.id.club_head:
                //头像点击
                List<String> headUrl = new ArrayList<>();
                headUrl.add(UChengApplication.userAvatar);
                UCommonUtil.getUtil().startImagePagerActivity(this.getContext(), OSSManager.OSS_HEAD, headUrl, 0, false);

                break;

            case R.id.club_info_image://显示二维码
                intent = new Intent(getContext(), QRCodeShowActivity.class);
                intent.putExtra(BaseConfig.TYPE, 1);
                intent.putExtra(BaseConfig.ID, UCommonUtil.getUtil().getId());
                break;

            case R.id.fmMe_fansNum_linear://粉丝
                intent = new Intent(getContext(), UsersActivity.class);
                intent.putExtra(BaseConfig.ID, UCommonUtil.getUtil().getId());
                intent.putExtra(BaseConfig.TYPE, 1);

                break;
            case R.id.fmMe_attenNum_linear://关注
//                intent = new Intent(this.getContext(), FansActivity.class);
//                intent.putExtra(BaseConfig.UID, UCommonUtil.getUtil().getId());
//                intent.putExtra(BaseConfig.TYPE, 2);
                intent = new Intent(getContext(), UsersActivity.class);
                intent.putExtra(BaseConfig.ID, UCommonUtil.getUtil().getId());
                intent.putExtra(BaseConfig.TYPE, 2);
                break;
//            case R.id.fmMe_friNum_linear://好友
////                intent = new Intent(this.getContext(), FansActivity.class);
////                intent.putExtra(BaseConfig.UID, UCommonUtil.getUtil().getId());
////                intent.putExtra(BaseConfig.TYPE, 3);
//                break;

//            case R.id.fmMe_qunNum_linear://群组
//                intent = new Intent(this.getContext(), MyGroupsActivity.class);
//                break;

//
//            case R.id.fmMe_club_access://纳新 社团
//                club_access_noRead.setVisibility(View.GONE);
//                intent = new Intent(this.getContext(), ClubAccessInfoActivity.class);
//
//                hideDot();
//                break;
//            case R.id.fmMe_club_speek://分享 社团
////                intent = new Intent(this.getContext(), MyReleaseActivity.class);
////                intent.putExtra(BaseConfig.TYPE, BaseConfig.U_CLUB_SHARE);
////                intent = new Intent(this.getContext(), MyReleaseDetailActivity.class);
////                intent.putExtra(BaseConfig.MUTI_TYPE, String.valueOf(BaseConfig.U_CLUB_SHARE));
//                break;
//            case R.id.fmMe_club_collect://收藏 社团
////                intent = new Intent(this.getContext(), MyCollectActivity.class);
////                intent.putExtra(BaseConfig.TYPE, 0);
//                break;

            case R.id.fmMe_club_release://发布 社团
                intent = new Intent(getContext(), LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/user/myshare");
                intent.putExtra(BaseConfig.URL_TITLE, "我的发布");
//                club_activity_noRead.setVisibility(View.GONE);
//                intent = new Intent(this.getContext(), MyReleaseDetailActivity.class);
//                hideDot();
                break;
//            case R.id.fmMe_club_apply://参与 社团
//
//                break;
            case R.id.fmMe_club_manager://社团管理
                intent = new Intent(getContext(), ClubManagerActivity.class);
                break;

            case R.id.fmMe_club_link://关联社圈
                intent = new Intent(getContext(), LinkCycleActivity.class);
                break;

//            case R.id.fmMe_club_info://资料 社团
//                intent = new Intent(this.getContext(), MyClubInfoActivity.class);
//                break;
//            case R.id.fmMe_club_member://成员管理 社团
//                intent = new Intent(this.getContext(), ClubMemberActivity.class);
//                intent.putExtra(BaseConfig.ORG_ID, UCommonUtil.getUtil().getId());
//                break;
//
//            case R.id.fmMe_club_transfer://转让社团管理权
//                intent = new Intent(getContext(), TransferCodeActivity.class);
//                break;


        }

        if (intent != null) {
            startActivity(intent);
        }
    }

    /*
    隐藏tab红点
    * */
    private void hideDot() {
        if (UChengApplication.userRole == BaseConfig.CLUB_ROLE) {
//            if (club_access_noRead.getVisibility() == View.GONE && club_share_noRead.getVisibility() == View.GONE && club_activity_noRead.getVisibility() == View.GONE && club_member_noRead.getVisibility() == View.GONE) {
//                ((ClubMainActivity) this.getActivity()).hideDot(4);
//            }
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        BaseConfig.changeGroup = false;
        FragmentUMe.isRefresh = false;
    }
}